﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;

namespace QLKH.PopUp.vattu
{
    public partial class popSuaDH : Form
    {
        private Int16 idDh = -1;
        public popSuaDH(Int16 id)
        {
            InitializeComponent();
            idDh = id;
        }

        private void binCombo(ComboBox cb, DataTable dt, string name, string value, string option)
        {
            List<trangthai> list = new List<trangthai>();
            trangthai ls = null;
            ls = new trangthai();
            ls.name = option;
            ls.value = "0";
            list.Add(ls);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ls = new trangthai();
                    ls.name = dt.Rows[i][name].ToString();
                    ls.value = dt.Rows[i][value].ToString();
                    list.Add(ls);
                }
            }
            cb.DataSource = list;
            cb.DisplayMember = "name";
            cb.ValueMember = "value";
        }

        private void popSuaDH_Load(object sender, EventArgs e)
        {
            clDH dh = new clDH();
            DataTable dtdh = dh.getdata();
            binCombo(cbbLoaiDH, dtdh, "ten", "id", "<Chọn loại đồng hồ>");

            DataTable dttl = dh.gettolap();
            binCombo(cbbTolap, dttl, "ten", "id", "<Chọn tổ lắp>");

            clKhachhang kh = new clKhachhang();
            DataTable dtkh = kh.getdata(3);
            binCombo(cbbKH, dtkh, "tenKH", "id", "<Khách hàng>");

            //clDHTong dht = new clDHTong();
            //DataTable dtdht = dht.getdata(1);
            //binCombo(cbbDHT, dtdht, "tenDHTong", "id", "<Chọn DHT>");

            clDH dhkh = new clDH();
            DataTable dtdhchitiet = dhkh.chitiet(idDh);
            if (dtdhchitiet != null && dtdhchitiet.Rows.Count > 0)
            {
                cbbKH.SelectedValue = dtdhchitiet.Rows[0]["idkh"].ToString();
                txtmaDh.Text = dtdhchitiet.Rows[0]["madh"].ToString();
                cbbLoaiDH.SelectedValue = dtdhchitiet.Rows[0]["idloai"].ToString();
                txtkichthuoc.Text =dtdhchitiet.Rows[0]["kichthuoc"].ToString();

                //dateset.Text = dtdhchitiet.Rows[0]["ngaylap"].ToString().Substring(4, 2) + "/" + dtdhchitiet.Rows[0]["ngaylap"].ToString().Substring(6, dtdhchitiet.Rows[0]["ngaylap"].ToString().Length-6) + "/" + dtdhchitiet.Rows[0]["ngaylap"].ToString().Substring(0, 4);
                cbbTolap.SelectedValue = dtdhchitiet.Rows[0]["idtolap"].ToString();

                txtchisoDH.Text = dtdhchitiet.Rows[0]["chiSoDH"].ToString();
                txtCSCDH1.Text = dtdhchitiet.Rows[0]["cscDH1"].ToString();
                txtCSMDH1.Text = dtdhchitiet.Rows[0]["csmDH1"].ToString();
                txtCSCDH2.Text = dtdhchitiet.Rows[0]["cscDH2"].ToString();
                checktrangthai.Checked = bool.Parse((dtdhchitiet.Rows[0]["trangthai"].ToString()));

                txtTongdutoan.Text = dtdhchitiet.Rows[0]["tongdutoan"].ToString();
                txtchitietdutoan.Text = dtdhchitiet.Rows[0]["chitietdutoan"].ToString();
                txtmotalapdat.Text = dtdhchitiet.Rows[0]["motaLapDat"].ToString();
                dateset.Text = dtdhchitiet.Rows[0]["ngaylap"].ToString().Substring(4, 2) + "/" + dtdhchitiet.Rows[0]["ngaylap"].ToString().Substring(6, dtdhchitiet.Rows[0]["ngaylap"].ToString().Length - 6) + "/" + dtdhchitiet.Rows[0]["ngaylap"].ToString().Substring(0, 4);
            }
        }

        public delegate void SuaDH(int outid);
        public SuaDH MyFunction;

        private void btsave_Click(object sender, EventArgs e)
        {
            if (MyFunction != null)
            {
                Int64 idkh = Int64.Parse(cbbKH.SelectedValue.ToString());
                string madh = txtmaDh.Text;
                Int16 loaiDH = Int16.Parse(cbbLoaiDH.SelectedValue.ToString());
                string kichthuoc = txtkichthuoc.Text;
                Int64 ngaylap = Int64.Parse(dateset.Text.Trim().Split('/')[2].ToString() + dateset.Text.Trim().Split('/')[1].ToString() + dateset.Text.Trim().Split('/')[0].ToString());
                Int16 idtotc = Int16.Parse(cbbTolap.SelectedValue.ToString());
                string tento = cbbTolap.Text;
                //Int16 idDHT = Int16.Parse(cbbDHT.SelectedValue.ToString());
                Int16 trangthai = Int16.Parse((checktrangthai.Checked) ? "1" : "0");

                //int chisoDH = int.Parse((txtchisoDH.Text.Trim() == "") ? "0" : txtchisoDH.Text.Trim());
                //float tongdutoan = float.Parse((txtTongdutoan.Text.Trim() == "") ? "0" : txtTongdutoan.Text.Trim());
                //string chitietdt = txtchitietdutoan.Text;
                //int cscdh1 = int.Parse((txtCSCDH1.Text.Trim() == "") ? "0" : txtCSCDH1.Text.Trim());
                //int csmdh1 = int.Parse((txtCSMDH1.Text.Trim() == "") ? "0" : txtCSCDH2.Text.Trim());
                //int cscdh2 = int.Parse((txtCSCDH2.Text.Trim() == "") ? "0" : txtCSMDH1.Text.Trim());

                Nullable<int> chisoDH;

                if (txtchisoDH.Text == "")
                    chisoDH = null;
                else
                    chisoDH = int.Parse(txtchisoDH.Text.Trim());

                Nullable<float> tongdutoan;

                if (txtTongdutoan.Text == "")
                    tongdutoan = null;
                else
                    tongdutoan = float.Parse(txtTongdutoan.Text.Trim());

                string chitietdt = txtchitietdutoan.Text;

                Nullable<int> cscdh1;

                if (txtCSCDH1.Text == "")
                    cscdh1 = null;
                else
                    cscdh1 = int.Parse(txtCSCDH1.Text.Trim());

                Nullable<int> csmdh1;

                if (txtCSMDH1.Text == "")
                    csmdh1 = null;
                else
                    csmdh1 = int.Parse(txtCSMDH1.Text.Trim());

                Nullable<int> cscdh2;

                if (txtCSCDH2.Text == "")
                    cscdh2 = null;
                else
                    cscdh2 = int.Parse(txtCSCDH2.Text.Trim());

                string motalap = txtmotalapdat.Text;

                clDH dhct = new clDH();
                int outid = dhct.updateDH(idDh, idkh, madh, loaiDH, kichthuoc, ngaylap, idtotc, tento, trangthai,chisoDH,tongdutoan,chitietdt,cscdh1,csmdh1,cscdh2,motalap);
                MyFunction(outid);
            }
            this.Close();
        }

        private void btquit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
